package com.novabluetooth.services.bluetooth2;

import com.novabluetooth.services.BluetoothConstant;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
/**
 * 蓝牙2.0服务
 * @author nova
 *
 */
public class ListenService extends Service {

	private final static String TAG = "ListenService";
	private BluetoothAdapter BluetAdapter;

	BluetoothChatService chatService;

//	private boolean playspeech = false;
//	private NovaTextToSpeech speech;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		BluetAdapter = BluetoothAdapter.getDefaultAdapter();

		chatService = new BluetoothChatService(getApplicationContext(),
				mHandler);
//		playspeech = true;
//		speech = new NovaTextToSpeech(this, "SYS");
	}

	@Override
	public void onStart(Intent intent, int startId) {

		if (chatService != null) {
			chatService.start();
		}
	}

	@Override
	public void onDestroy() {

		if (chatService != null) {
			chatService.stop();
		}
		// if (speech != null) {
		// speech.stop();
		// }
		super.onDestroy();
	}

	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			boolean isvisible = ListenService.this.getBluetoothViewVisible();
			switch (msg.what) {
			case MessageValue.MESSAGE_STATE_CHANGE:
				switch (msg.arg1) {
				case BluetoothChatService.STATE_NONE:
					Log.i(TAG, "蓝牙无连接");
					break;
				case BluetoothChatService.STATE_LISTEN:
					Log.i(TAG, "蓝牙开始连接");
					break;
				case BluetoothChatService.STATE_CONNECTING:
					Log.i(TAG, "蓝牙连接中......");
					break;
				case BluetoothChatService.STATE_CONNECTED:
					Log.i(TAG, "蓝牙已连接");
					break;
				}
				break;

			case MessageValue.MESSAGE_TOAST:

				// 蓝牙连接状态断开弹出的提示信息
				break;

			case MessageValue.MSG_READ_BT:
				Log.e("LeeTag", "BT_MSG");
//				if (isvisible) {
//					setBluetoothViewVisible(false);
//					Bundle btBundle = msg.getData();
//					Intent btIntent = new Intent(ListenService.this,
//							BTResultActivity.class);
//					btIntent.putExtra(BTResultActivity.BT_BUNDLE, btBundle);
//					btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					// btIntent.setAction("com.nova.novanationalhealthy.tools.bluetooth.BTActivity");
//					startActivity(btIntent);
//					// sendBroadcast(btIntent);
//					//speak(btBundle, MessageValue.MSG_READ_BT);
//				}
				break;

			case MessageValue.MSG_READ_BG:
			case MessageValue.MSG_READ_BG_PA:
				Log.e("LeeTag", "BG_MSG");
				if (isvisible) {
					setBluetoothViewVisible(false);
					Bundle bgBundle = msg.getData();
					System.out.println(msg.getData().toString());
					Intent bgIntent = new Intent(ListenService.this,
							BGResultActivity.class);
					bgIntent.putExtra(BGResultActivity.BG_BUNDLE, bgBundle);
					bgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					// btIntent.setAction("com.nova.novanationalhealthy.tools.bluetooth.BTActivity");
					startActivity(bgIntent);
//					 sendBroadcast(btIntent);
//					speak(bgBundle, MessageValue.MSG_READ_BG);
				}
				break;

			case MessageValue.MSG_READ_TF:
				Log.e("LeeTag", "TF_MSG");
//				if (isvisible) {
					setBluetoothViewVisible(false);
					Bundle tfBundle = msg.getData();
//					Intent tfIntent = new Intent(ListenService.this,
//							TFResultActivity.class);
//					tfIntent.putExtra(TFResultActivity.TF_BUNDLE, tfBundle);
//					tfIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					// btIntent.setAction("com.nova.novanationalhealthy.tools.bluetooth.BTActivity");
//					startActivity(tfIntent);
					// sendBroadcast(btIntent);
					//speak(tfBundle, MessageValue.MSG_READ_TF);
					Intent intent=new Intent();
					intent.setAction(BluetoothConstant.REGISTER_EW);
					intent.putExtra("ewValue", tfBundle);
					sendBroadcast(intent);
					
//				}
				break;
			case MessageValue.MSG_READ_TZ:
				Log.e("LeeTag", "TZ_MSG");
//				if (isvisible) {
//					setBluetoothViewVisible(false);
//					Bundle tfBundle = msg.getData();
//					Intent tfIntent = new Intent(ListenService.this,
//							TZResultActivity.class);
//					tfIntent.putExtra(TZResultActivity.TZ_BUNDLE, tfBundle);
//					tfIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					// btIntent.setAction("com.nova.novanationalhealthy.tools.bluetooth.BTActivity");
//					startActivity(tfIntent);
//					// sendBroadcast(btIntent);
//					//speak(tfBundle, MessageValue.MSG_READ_TF);
//				}
				break;

			case MessageValue.MSG_READ_XT_PA:
				Log.e("LeeTag", "XT_MSG");
//				if (isvisible) {
//					setBluetoothViewVisible(false);
//					Bundle xtBundle = msg.getData();
//					Intent xtIntent = new Intent(ListenService.this,
//							XTResultActivity.class);
//					xtIntent.putExtra(XTResultActivity.XT_BUNDLE, xtBundle);
//					xtIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					// btIntent.setAction("com.nova.novanationalhealthy.tools.bluetooth.BTActivity");
//					startActivity(xtIntent);
//					// sendBroadcast(btIntent);
//					//speak(tfBundle, MessageValue.MSG_READ_TF);
//				}
				break;
				
			case MessageValue.MSG_READ_XZ_PA:
				Log.e("LeeTag", "XZ_MSG");
//				if (isvisible) {
//					setBluetoothViewVisible(false);
//					Bundle xzBundle = msg.getData();
//					Intent xzIntent = new Intent(ListenService.this,
//							XZResultActivity.class);
//					xzIntent.putExtra(XZResultActivity.XZ_BUNDLE, xzBundle);
//					xzIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					// btIntent.setAction("com.nova.novanationalhealthy.tools.bluetooth.BTActivity");
//					startActivity(xzIntent);
//					// sendBroadcast(btIntent);
//					//speak(tfBundle, MessageValue.MSG_READ_TF);
//				}
				break;
				
			case MessageValue.MSG_READ_BU:
				Log.e("LeeTag", "BU_MSG");
//				if (isvisible) {
//					setBluetoothViewVisible(false);
//					Bundle buBundle = msg.getData();
//					Intent buIntent = new Intent(ListenService.this,
//							BUResultActivity.class);
//					buIntent.putExtra(BUResultActivity.BU_BUNDLE, buBundle);
//					
//					System.out.println("测试项目："+buBundle.getString("bu_item")+"测量值：   "+buBundle.getString("bu_value"));
//					
//					buIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					// btIntent.setAction("com.nova.novanationalhealthy.tools.bluetooth.BTActivity");
//					startActivity(buIntent);
//					// sendBroadcast(btIntent);
//					//speak(tfBundle, MessageValue.MSG_READ_TF);
//				}
				break;
			case MessageValue.MSG_READ_UR:
//				Log.e("LeeTag", "MSG_READ_UR");
//				if (isvisible) {
//					setBluetoothViewVisible(false);
//					Bundle buBundle = msg.getData();
//					Intent buIntent = new Intent(ListenService.this,
//							NiaoYeResultActivity.class);
//					buIntent.putExtra("bundle", buBundle);
//					
//					System.out.println("测试项目："+buBundle.getString("data")+"测量值：   "+buBundle.getString("bu_value"));
//					
//					buIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//					// btIntent.setAction("com.nova.novanationalhealthy.tools.bluetooth.BTActivity");
//					startActivity(buIntent);
//					// sendBroadcast(btIntent);
//					//speak(tfBundle, MessageValue.MSG_READ_TF);
//				}
				break;
				
				
				
			}
		}
	};

	/**
	 * 设置测量结果是否显示
	 * 
	 * @param flag
	 */
	public void setBluetoothViewVisible(boolean flag) {
		SharedPreferences sp = this.getSharedPreferences("user", MODE_PRIVATE);
		sp.edit().putBoolean("BluetoothView", flag).commit();
	}

	/**
	 * 获取测量结果是否显示
	 */
	public boolean getBluetoothViewVisible() {
		SharedPreferences sp = this.getSharedPreferences("user", MODE_PRIVATE);
		return sp.getBoolean("BluetoothView", true);
	}
}
